<?php
namespace app\common\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Db;

class IncomeReport extends Command
{
    protected function configure()
    {
        $this->setName('income_stats')
            ->addArgument('action', Argument::OPTIONAL, "方法名称")
            ->addOption('start_date', null, Option::VALUE_REQUIRED, '开始日期')
            ->addOption('end_date', null, Option::VALUE_REQUIRED, '结束日期')
            ->setDescription('充值金额汇总');
    }

    protected function execute(Input $input, Output $output)
    {

        $action = $input->getArgument('action');

        if (method_exists($this, $action)) {

            $start_date = $input->hasOption('start_date') ? $input->getOption('start_date') : date('Y-m-d', strtotime('-1days'));
            $end_date = $input->hasOption('end_date') ? $input->getOption('end_date') : date('Y-m-d', strtotime('-1days'));

            $this->$action($start_date, $end_date);
        }

    }

    /*充值金额汇总 -- 投放号维度*/
    public function daily_channel($start_date,$end_date)
    {
        if ($start_date == $end_date) {
            $start_date = date('Y-m-d', strtotime('-1days'));
            $end_date = date('Y-m-d', strtotime('-1days'));
        }

        $date_rang = getDateByRange($start_date, $end_date);

        foreach ($date_rang as $date) {

            $list = Db::table('pay_order_success')
                ->where('status',2)
                ->where('create_date',$date)
                ->field('SUM(total_fee) AS total_fee,channel_id')->group('channel_id')->select();

            if($list)
            {
                foreach ($list as $lv)
                {

                    $ratio = Db::table('cms_manager_user')->where('id',$lv['channel_id'])->field('sharing_ratio')->findOrEmpty();
                    $sharing_ratio = $ratio ? $ratio['sharing_ratio']/100 : 0;

                    $record = Db::table('rp_income_summary_daily_channel')
                        ->where(['channel_id'=>$lv['channel_id'],'report_date'=>$date])->field('id')->find();

                    if($record){

                        Db::table('rp_income_summary_daily_channel')->where(['id'=>$record['id']])->update([
                            'recharge_amount'               => $lv['total_fee'],
                            'commission_amount'             => floor($lv['total_fee']*$sharing_ratio),
                        ]);

                    }else{

                        Db::table('rp_income_summary_daily_channel')->insert([
                            'recharge_amount'               => $lv['total_fee'],
                            'commission_amount'             => floor($lv['total_fee']*$sharing_ratio),
                            'channel_id'                    => $lv['channel_id'],
                            'report_date'                   => $date
                        ]);
                    }

                }
            }


        }

    }

    /*充值金额汇总 -- 代理维度*/
    public function daily_agent($start_date,$end_date)
    {
        if ($start_date == $end_date) {
            $start_date = date('Y-m-d', strtotime('-1days'));
            $end_date = date('Y-m-d', strtotime('-1days'));
        }

        $date_rang = getDateByRange($start_date, $end_date);

        $roleId = Db::table('cms_manager_role')->where('alias','agent')->value('id');
        $agents = Db::table('cms_manager_user')->where('role_id',$roleId)->column('id');

        foreach ($agents as $agent_id)
        {
            $channelIds = Db::table('cms_manager_user')->where('parent_id',$agent_id)->column('id');
            if(empty($channelIds))
            {
                continue;
            }

            $channelIdStr = implode(',',$channelIds);

            foreach ($date_rang as $v) {

                $sql = "SELECT 
                   report_date,SUM(recharge_amount) as recharge_amount,SUM(commission_amount) as commission_amount                    
                FROM `rp_income_summary_daily_channel` 
                WHERE report_date = '{$v}' AND channel_id in ({$channelIdStr})
                GROUP by report_date";

                $result = Db::query($sql);

                if($result){

                    $record = Db::table('rp_income_summary_daily_agent')
                        ->where(['report_date'=>$v,'agent_id'=>$agent_id])
                        ->field('id')->find();

                    if($record){

                        Db::table('rp_income_summary_daily_agent')->where(['id'=>$record['id']])->update([
                            'recharge_amount'              => $result[0]['recharge_amount'],
                            'commission_amount'            => $result[0]['commission_amount'],
                        ]);

                    }else{

                        Db::table('rp_income_summary_daily_agent')->insert([
                            'recharge_amount'              => $result[0]['recharge_amount'],
                            'commission_amount'            => $result[0]['commission_amount'],
                            'report_date'                  => $v,
                            'agent_id'                     => $agent_id
                        ]);
                    }

                }
            }
        }

        foreach ($date_rang as $v) {

            $sql = "SELECT 
                   report_date,SUM(recharge_amount) as recharge_amount,SUM(commission_amount) as commission_amount                    
                FROM `rp_income_summary_daily_channel` 
                WHERE report_date = '{$v}' AND channel_id = 0
                GROUP by report_date";

            $result = Db::query($sql);

            if($result){

                $record = Db::table('rp_income_summary_daily_agent')
                    ->where(['report_date'=>$v,'agent_id'=>0])
                    ->field('id')->find();

                if($record){

                    Db::table('rp_income_summary_daily_agent')->where(['id'=>$record['id']])->update([
                        'recharge_amount'              => $result[0]['recharge_amount'],
                        'commission_amount'            => $result[0]['commission_amount'],
                    ]);

                }else{

                    Db::table('rp_income_summary_daily_agent')->insert([
                        'recharge_amount'              => $result[0]['recharge_amount'],
                        'commission_amount'            => $result[0]['commission_amount'],
                        'report_date'                  => $v,
                        'agent_id'                     => 0
                    ]);
                }

            }
        }



    }

}